package backend;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import logs.LogFactory;

import org.apache.log4j.Logger;

public class Util {
  private static final Logger logger = LogFactory.getLog(Util.class);
  public static String administratorVerification = "Adm1nV3r1";
  
  public static boolean noNulls(final Object... objects) {
    for (final Object o : objects)
      if (null == o)
        return false;
    return true;
  }
  
  public static void tryClose(final Object... objects) {
    for (final Object o : objects)
      if (null != o)
        try {
          if (o instanceof PreparedStatement)
            close((PreparedStatement) o);
          else if (o instanceof ResultSet)
            close((ResultSet) o);
          else if (o instanceof Statement)
            close((Statement) o);
          else if (o instanceof Connection)
            close((Connection) o);
          else if (o instanceof Closeable)
            close((Closeable) o);
          else
            logger.warn("Object " + o + " doesn't support closing");
        } catch (final Exception e) {
          logger.error("failed to close object " + o, e);
        }
  }
  
  private static void close(final Connection conn) throws SQLException {
    conn.close();
  }
  
  private static void close(final Statement stmt) throws SQLException {
    stmt.close();
  }
  
  private static void close(final PreparedStatement stmt) throws SQLException {
    stmt.close();
  }
  
  private static void close(final ResultSet rs) throws SQLException {
    rs.close();
  }
  
  private static void close(final Closeable c) throws IOException {
    c.close();
  }
}
